package com.csu.hemiao.service.impl;

import com.csu.hemiao.common.CommonResponse;
import com.csu.hemiao.domain.Cart;
import com.csu.hemiao.domain.User;
import com.csu.hemiao.persistence.UserMapper;
import com.csu.hemiao.service.UserService;
import com.csu.hemiao.service.RedisService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private RedisService redisService;

    @Autowired
    private UserMapper userMapper;

    //登录功能
    @Override
    public CommonResponse<JSONObject> login(int account, String password, String inputValidCode) throws JSONException {
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user = userMapper.getUserByAccountAndPassword(user);

        String rightValidCode = (String) redisService.get("rightValidCode");

        if (inputValidCode.length() != 4){
            //验证码格式不通过
            return CommonResponse.createForError("Wrong verification code!");
        }
        else{
            //验证码格式通过
            int inputValidCode1 = inputValidCode.charAt(0) - 48;
            int inputValidCode2 = inputValidCode.charAt(1) - 48;
            int inputValidCode3 = inputValidCode.charAt(2) - 48;
            int inputValidCode4 = inputValidCode.charAt(3) - 48;


            if ((inputValidCode1 != rightValidCode.charAt(0)) || (inputValidCode2 != rightValidCode.charAt(1)) ||
                    (inputValidCode3 != rightValidCode.charAt(2)) || (inputValidCode4 != rightValidCode.charAt(3))){
                //验证码内容不通过
                return CommonResponse.createForError("Wrong verification code!");
            }
            else if (user == null){
                //密码错误
                return CommonResponse.createForError("Wrong password!");
            }
            else{
                //密码正确
                //将token和user加入缓存
                String token = UUID.randomUUID().toString();
                redisService.set(token, account);
                redisService.set("user",user);

                //将用户基本信息传给前端
                JSONObject info = new JSONObject();
                info.put("token",token);
                info.put("name",user.getName());
                info.put("receiveAddr",user.getReceiveAddr());
                info.put("account",user.getAccount());
                info.put("userType",user.getUserType());
                info.put("phoneNum",user.getPhoneNumber());

                //如果用户是买家，初始化购物车
                if (user.getUserType() == "BUYER"){
                    Cart cart = new Cart();
                    //购物车加入缓存
                    redisService.set("cart",cart);
                }

                System.out.println(info);
                return CommonResponse.createForSuccess(info);
            }

        }
    }
    //查看用户信息功能

    @Override
    public CommonResponse<JSONObject> checkUserInfo() throws JSONException {
        //获取缓存中的user
        User user = (User) redisService.get("user");

        //将基本用户信息传给前端
        JSONObject info = new JSONObject();
        info.put("name",user.getName());
        info.put("receiveAddr",user.getReceiveAddr());
        info.put("account",user.getAccount());
        info.put("userTpe",user.getUserType());
        info.put("phoneNum",user.getPhoneNumber());

        //如果用户是卖家，搜索发货地址发给前端
        if (user.getUserType() == "SELLER"){
            String shipAddr = userMapper.getSellerShipAddr(user.getAccount());
            info.put("shipAddr",shipAddr);
        }

        //如果用户是管理员，搜索管理员账号发给前端
        if (user.getUserType() == "ADMIN"){
            int adAccount = userMapper.getAdAccount(user.getAccount());
            info.put("adAccount",adAccount);
        }

        System.out.println(info);
        return CommonResponse.createForSuccess(info);
    }
}
